package com.fhnew.app.ui.dialog;


import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.fhnew.app.util.HLog;
import com.fhnew.app.widget.CheckOutView;

/**
 * 用于显示滑动验证的对话框
 */
public class CheckOutDialog extends DialogFragment {

    // 配置对话框的宽高
    private int width  = 240;
    private int height  = 140;
    private int padding = 5;

    private String bgUrl;//传入图像的URL
    private int bgWidth ;//传入图像的宽度
    private int bgHeight ;//传入图像的高度

    private String blockUrl;//传入滑块图像的URL
    private int blockWidth;//传入滑块图像的宽度
    private int blockHeight; //传入滑块图像的高度

    private int blockY  = 50; //滑块Y轴的高度

    private CheckOutView checkOutView;
    private CheckOutView.CheckOutListener listener;

    public void init(String bgUrl,int bgWidth,int bgHeight,String blockUrl,int blockWidth,int blockHeight,int blockY){
        this.bgUrl = bgUrl;
        this.bgWidth = bgWidth;
        this.bgHeight = bgHeight;
        this.blockUrl = blockUrl;
        this.blockWidth = blockWidth;
        this.blockHeight = blockHeight;
        this.blockY = blockY;
    }

    private DisplayMetrics displayMetrics;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        displayMetrics = getResources().getDisplayMetrics();
        //设置透明图片为背景
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置背景透明度 范围是0.1f-1.0f
        getDialog().getWindow().setDimAmount(0.1f);

        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadius(padding*displayMetrics.density);
        gradientDrawable.setColor(Color.WHITE);

        // 布局
        LinearLayout layout = new LinearLayout(getContext());
        ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(120,0,120,0);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackground(gradientDrawable);
        layout.setLayoutParams(layoutParams);


        checkOutView = new CheckOutView(getContext(),bgUrl,bgWidth,bgHeight,blockUrl,blockWidth,blockHeight,blockY);
        checkOutView.setCheckOutListener(checkOutListener);


        layoutParams = new ViewGroup.MarginLayoutParams((int)(width*displayMetrics.density - displayMetrics.density * padding * 2), (int) (height*displayMetrics.density-displayMetrics.density*padding*2));
        //HLog.I("设置 滑动控件尺寸"+(width - (int)(displayMetrics.density * padding * 2))+"  "+((int) (height-displayMetrics.density*padding*2)));
        layoutParams.setMargins((int)(displayMetrics.density * padding ),(int)(displayMetrics.density * padding ),(int)(displayMetrics.density * padding ),(int)(displayMetrics.density * padding ));
        checkOutView.setLayoutParams(layoutParams);

        layout.addView(checkOutView);

        return layout;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        //去掉dialog默认的padding
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (width*displayMetrics.density);
        lp.height = (int) (height*displayMetrics.density);

        // HLog.I("对话框尺寸"+lp.width+"  "+lp.height);

        window.setAttributes(lp);
        //只有设置了setBackgroundDrawable才可以填充满整个屏幕
        window.setBackgroundDrawable(new ColorDrawable());
    }

    public void setCheckOutListener (CheckOutView.CheckOutListener listener){
        this.listener = listener;
    }

    private CheckOutView.CheckOutListener checkOutListener = new CheckOutView.CheckOutListener() {
        @Override
        public void seekEnd(float value) {
            if(listener!= null){
                listener.seekEnd(value);
            }
        }
    };
}
